#pragma once

#include <QString>
#include <QList>
#include <QColor>
#include <QSharedPointer>
#include <QMetaType>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QMetaType>

#define DATE_TIME_FORMAT_STRING "yyyy-MM-dd hh:mm:ss"
#define MAX_LINE_SIZE 100

#define WEBAPI_DataBatchUpload  "/api/DataBatchUpload"
#define WEBAPI_DataQuery  "/api/DataQuery"

#define WEBAPI2_DataQuery  "/api/HDQuery"
#define WEBAPI2_DataUpload  "/api/DataUpload"

#define CZ_WEBAPI_DataBatchUpload  "/api/DataBatchUpload"

#define WEBAPI_HM_DataBatchUpload  "/api/DataBatchUpload"

#define barcode_key    "barcode_key"
#define test_time_key  "test_time_key"
#define model_no_key  "model_no_key"
#define upload_key     "upload_key"
#define machine_key    "machine_key"
#define message_key    "message_key"

namespace CommonComponet
{
	struct CheckItem
	{
		/*{
			"KeyName": "正极电阻",
			"PlcKey" : "irtp",
			"SelfKey" : "meter1",
			"isActive" : true,
			"isShowResult" : true
		  }
		*/

		QString KeyName;
		QString PlcKey;
		QString SelfKey;
		bool isActive;
		bool isShowResult;
	};

	enum SiteDeviceSn
	{
		SDS_INVALID,
		SDS_ScanQrCode,   //扫码
		SDS_PLC,          //PLC
		DEVICE_NUM
	};

	struct UcDataDetection
	{
		int id;
		QString uid;
		QString barcode;
		QString data_time;
		QString result;
		QString model;
		QString site;
		QString message;
		QString line;
		int	    upload;

		QString working_procedure; //RDF 工序

		QString modelNo; //模号
		QString usEnergy; 
		QString uspower; // RDF 功率
		QString usAmplitude;
		QString usWeldTime;
		QString solidifyTime;
		QString delayTime;

		QString machineNo;

		int accessIndex;


	};
	Q_DECLARE_METATYPE(UcDataDetection);

	struct HeliumDetectionDefine
	{
		int      id;
		QString  uid;
		QString  barcode;
		QString  message;
		QString  datetime;
		QString  site;
		QString  line;
		int      upload;
		QString  model;

		QString  modelNo;
		QString  hdTime;
		QString  hdData;
		QString  hdState;
		QString  hdTestTime;
		QString  ng_Message;

		QString hd_KQ;         // 空气压力
		QString hd_VCM;        // 真空压力
		QString hold_time;     // 氦检保压时间
		QString hd_pressure;   // 氦气压力

		QString machineNo;
	};
	Q_DECLARE_METATYPE(HeliumDetectionDefine);

	struct ElectricalDetectionDefine
	{
		int id;
		QString uid;
		QString barcode;
		QString datetime;
		QString message;
		QString site;
		QString model;
		QString result;
		QString line;
		int     upload;
		bool    warning;

		bool fool_proof;

		int meter1Type;
		QString meter1Time;
		QString meter1Data;
		QString meter1State;

		int meter2Type;
		QString meter2Time;
		QString meter2Data;
		QString meter2State;

		int meter3Type;
		QString meter3Time;
		QString meter3Data;
		QString meter3State;
		int meter3Result;

		int meter4Type;
		QString meter4Time;
		QString meter4Data;
		QString meter4State;

		int meter5Type;
		QString meter5Time;
		QString meter5Data;
		QString meter5State;

		int meter6Type;
		QString meter6Time;
		QString meter6Data;
		QString meter6State;

		int meter7Type;     //正极电阻
		QString meter7Time;
		QString meter7Data;
		QString meter7State;

		int meter8Type;     //负极电阻
		QString meter8Time;
		QString meter8Data;
		QString meter8State;

		QString flatData;
		QString flatState;

		QString pHeightData;
		QString pHeightState;

		QString nHeightData;
		QString nHeightState;

		QString modelNo;
		QString machineNo;
	};
	Q_DECLARE_METATYPE(ElectricalDetectionDefine);

	struct TestItem
	{
		QString tile;
		QString plc_key;
		QString name_key;
		QString test_value;
		QString test_result;

		QVariant toVariant() const
		{
			QVariantMap map;
			map.insert("tile", tile);
			map.insert("plc_key", plc_key);
			map.insert("name_key", name_key);
			map.insert("test_value", test_value);
			map.insert("test_result", test_result);
			return map;
		}

		void fromVariant(const QVariant& variant)
		{
			QVariantMap map = variant.toMap();
			tile = map.value("tile").toString();
			plc_key = map.value("plc_key").toString();
			name_key = map.value("name_key").toString();
			test_value = map.value("test_value").toString();
			test_result = map.value("test_result").toString();
		}
	};
	Q_DECLARE_METATYPE(CommonComponet::TestItem);

	struct BasicDetectionDefine
	{
		QString funcNo; // 业务ID  同共享

		int      id;
		QString  uid;       //guid
		QString  barcode;   //条码
		QString  message;   //消息
		QString  date_time;  //记录时间
		QString  site;      //站点
		QString  line;      //Line
		int      upload;

		QString  model;     //产品类型
		QString  modelNo;   //模号

		QString  workingProcedure;  //工序
		QString  machineNo;         //机台号

		QString plc_result;
		QString test_item_string;

		// 1.极柱毛刺检测 + 条码 + 2. 防爆片蓝膜检测3.成品盖板检测4.极柱检测
		//OK;条码格式;OK;OK;OK;
		QMap<QString, TestItem> Test_ItemMaps;
		//QString  j;
		//QString  bigleakData;
		//QString  bigleakState;
		//QString  bigleakTestTime;
		//QString  ng_Message;
	};
	Q_DECLARE_METATYPE(BasicDetectionDefine);

	struct EpsWelding_detection_define:public BasicDetectionDefine
	{
		double d_welding_time; //内存使用。。。用于计算

		QString g_power; //光纤功率
		QString b_power; //半导体功率

		double g_power_value_min = 9999;
		double g_power_value_max = -9999;

		double b_power_value_min = 99999;
		double b_power_value_max = -99999;

	};
	Q_DECLARE_METATYPE(EpsWelding_detection_define);

	enum DeviceCommType
	{
		CCT_INVALID,
		CCT_COM,
		CCT_TCP_IP,
		CCT_MODBUS_TCP,
		CCT_MC_TCP,
		CCT_ALL
	};

	enum DeviceConnState
	{
		UnconnectedState,
		ConnectingState,
		ConnectedState,
		ClosingState,
		ConnectTimeOut
	};

	struct SerialInfo
	{
		QString comPort;
		unsigned int baudRate;
		unsigned int dataBits;
		unsigned int parity;
		unsigned int stopBits;
		unsigned int flowControl;
	};

	struct TcpConnectInfo
	{
		QString ip;
		int port;
	};

	struct DeviceConfig
	{
		QString device_name;
		DeviceCommType comm_type;
		QSharedPointer<SerialInfo> serical_info;
		QSharedPointer<TcpConnectInfo> tcp_info;
	};

	/// <summary>
	/// TCP发送数据的形式 二进制 文本
	/// </summary>
	enum DataMode {
		TextMode,    // 文本模式(默认)
		BinaryMode   // 二进制模式
	};

	//PLC寄存器类型
	enum PlcRegisterType
	{
		Invalid,          //未定义      0
		DiscreteInputs,   //开关量输入  1
		Coils,            //线圈 位地址 2
		InputRegisters,   //输入寄存器  3
		HoldingRegisters  //保持寄存器  4   1个寄存器两个字
	};

	//PLC读写地址定义
	struct PlcIoAddrDefine
	{
		PlcRegisterType ctype;
		unsigned int startAddress;
		unsigned int numberOfEntries;
	};

	struct PlcIoData  //PLC读写数据接口
	{
		PlcIoData()
		{
			id = -1;
			isForbidden = false;
			completeTag = false;
			canReadWriteTag = false;
			m_value.clear();
			blongProName = "PLC";
			isSigAddr = false;
			isOutterCode = false;
			stationNo = 0;
			blongStation = 0;
			isHeartBeat = false;
			hbIsActive = false;
			addrIndex = -1;
			resetTag = false;
			nextReadAddrId = -1;
			nextReadAddrNum = 1;
			nextWriteAddrId = -1;
			nextWriteAddrNum = 1;
			nextPlcIoData = NULL;
			initValue(1);
		}

		void initValue(unsigned int count)
		{
			for (unsigned int i = 0; i < count; i++)
			{
				m_value.append(0);
			}
			for (unsigned int i = 0; i < 3; i++)
			{
				k_value.append(0);
			}
		}

		int id;
		bool isForbidden;
		bool isSigAddr;                 //是否是各工序的信号点
		QString noteName;               // 所属工序 默认是 PLC
		QString blongProName;            // 所属设备名称 默认是 PLC
		QString plcKey;            // 所属设备名称 默认是 PLC
		bool isOutterCode;
		int  stationNo;
		int  blongStation;
		bool isHeartBeat;
		bool hbIsActive;   //心跳包是否需要
		bool resetTag;
		PlcIoAddrDefine b_p_addr;        //所属循环地址
		PlcIoAddrDefine m_p_addr;        //地址
		int  addrIndex;
		//bool canReadWriteTag; //可读可写标志
		std::atomic<bool> canReadWriteTag{ false };
		bool completeTag;
		QVector<quint16> m_value;        //16位数值
		QVector<quint16> reset_value;        //16位数值
		int nextReadAddrId;
		int nextReadAddrNum;
		int nextWriteAddrId;
		int nextWriteAddrNum;
		QSharedPointer<PlcIoData> nextPlcIoData;
		QVector<quint16> k_value;        //16位数值
	};

	struct PlcGeneralAddrs
	{
		QSharedPointer<PlcIoData> m_hbAddr;
		QVector<QSharedPointer<PlcIoData>> m_loopAddrList;
		QVector<QSharedPointer<PlcIoData>> m_signalAddrList;
		QVector<QSharedPointer<PlcIoData>> m_stateAddrList;
		QVector<QSharedPointer<PlcIoData>> m_alarmAddrList;
		QVector<QSharedPointer<PlcIoData>> m_plcReadAddrList;
		QVector<QSharedPointer<PlcIoData>> m_plcWriteAddrList;
	};

	struct PlcDeviceSetting
	{
		DeviceCommType comm_type;
		QSharedPointer<TcpConnectInfo> tcp_config;
		PlcGeneralAddrs plcAddrs;
		QString plc_name;

		int m_offset;
		int d_offset;
	};

	struct SiteConfigInfo
	{
		QString site_id;
		QString site_name;
		QString ini_file_path;
		QString plc_xml_config_file_path;
		QString plc_xml_config_name;
		QSharedPointer<PlcDeviceSetting> plc_config;
		QVector<QSharedPointer<DeviceConfig>> devices;
	};

#define  WD_BOARD_TYPE 1 
#define WD_POSITIVE_TYPE 2
#define WD_NEGATIVE_TYPE 3
#define NORMAL_COLOR 100, 149, 237
#define FAIL_COLOR 236,17,72
#define OK_COLOR 87,191,20
#define WARING_COLOR 247,243,20
#define NG_COLOR 236,17,72

	enum DEVICE_CONN_TYPE
	{
		ASYNC_AND_TIME_OUT, //Qt::QueuedConnection //timeout return
		ASYNC,  //Qt::QueuedConnection
		SYNC_FAQ_WAIT  //Qt::BlockingQueuedConnection
	};

	struct shipment_data
	{
		QString SN;
		QString barcode;
		QSharedPointer<HeliumDetectionDefine> helium_detection_data;
		QSharedPointer<ElectricalDetectionDefine> electrical_detection_data;
		bool bResult; //NG OR OK
		QString barcode_mode; //产品类型
	};

	enum UI_CLENT_TYPE
	{
		CZ_CLIENT_TAG_MES_SITE,      //池州
		CZ_CLIENT_TAG_MES_SHIPMENT,  //池州扫码出货

		ZZ_CLIENT_TAG_MES_SITE,      //中泽

		ZZ_CLIENT_TAG_MES_SHIPMENT,  //中泽扫码出货

		JRP_EDCHECK_CLIENT_TAG_MES_SITE,  //JRP电测
		JRP_HDCHECK_CLIENT_TAG_MES_SITE,  //JRP氦检

		ZZ_CLIENT_TAG_MES_SITE_SINGLE_EN_,      //中泽 单站点刻码
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_EPS_,      //中泽 单站点防爆片
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_BC_,      //中泽 单站点极柱焊接
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_UC_,      //中泽 单站点超声波
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_HD_,      //中泽 单站点氦检
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_REPAIR_WELD, //中泽 补焊扫码
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_DA_EPS,      //中泽 大德 制样线 防爆片焊接
		ZZ_CLIENT_TAG_MES_SITE_SINGLE_DA_PIN_BC,      //中泽 大德 引脚极柱焊接 047b
		ZZ_CLIENT_TAG_MES_SITE_BIND_MATERIAL_MODEL,  //中泽 产线绑定物料条码
		ZZ_CLIENT_TAG_MES_SITE_AUTO_SINGLE_BC,  //中泽 单机自动扫码 极柱焊接
		DA_CLIENT_TAG_MES_SITE_AUTO_SINGLE_BC_TYPE_1,  //大德激光焊接 单机自动扫码 极柱焊接1
		ZZ_SEBL_CLIENT_TAG_MES_SITE,      //中泽 SHENBAO
		
		TM_CLIENT_TAG_MES_SITE,      //腾美

		TM_CLIENT_TAG_MES_ZZ_QY_ED,      //中泽 乾叶 MES 电测客户端

		TM_CLIENT_TAG_MES_ZZ_BR_ED,      //中泽 博然 MES 电测客户端

		YL_CLIENT_TAG_MES_DATA_VIEW,      //YL 四川赢立 数据查看 已弃用

		HM_CODE_CLIENT_TAG_MES_SITE,     //崇州 HM

		JIUAN_CLIENT_TAG_MES_SITE,     //久安 

		NONE_CLIENT_TAG_MES_SITE,    //其它客户端
	};
}